package com.bhs.sansong.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansong.R;
import com.bhs.sansong.bean.Outlets;
import com.bhs.sansong.utils.MyUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
 * 选择网点
 */
public class OutletsDialog extends Dialog {


    private RecyclerView mRvList;
    private BaseQuickAdapter<Outlets, BaseViewHolder> mAdapter;

    public OutletsDialog(@NonNull Context context) {
        super(context, R.style.MyBottomDialogStyle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_outlets);
        Window window = getWindow();
        //底部弹出的Dialog
        window.setGravity(Gravity.BOTTOM);
        //设置动画
        window.setWindowAnimations(R.style.main_menu_animstyle);
        WindowManager.LayoutParams attributes = window.getAttributes();

        //保证按钮水平满屏
        attributes.width = ViewGroup.LayoutParams.MATCH_PARENT;
        attributes.height = MyUtils.getScreenHeight(getContext()) * 5 / 10;

        //设置显示位置
        window.setAttributes(attributes); //将属性设置给窗体
        initView();
    }

    private void initView() {
        mRvList = findViewById(R.id.rv_list);
        mAdapter = new BaseQuickAdapter<Outlets, BaseViewHolder>(R.layout.item_outlets) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, Outlets item) {
                TextView tvName = helper.getView(R.id.tv_name);
                TextView tvPhone = helper.getView(R.id.tv_phone);
                TextView tvAddress = helper.getView(R.id.tv_address);
                tvName.setText(String.format("网点%s：%s", helper.getLayoutPosition() + 1, item.getOutlets_name()));
                tvPhone.setText(String.format("电话：%s", MyUtils.phoneHide(item.getOutlets_mobile())));
                tvAddress.setText(String.format("地址：%s", item.getOutlets_address()));
                if (item.isChecked()) {
                    helper.setBackgroundRes(R.id.cl_main, R.drawable.bg_blue_8);
                    tvName.setTextColor(Color.parseColor("#FFFFFF"));
                    tvPhone.setTextColor(Color.parseColor("#FFFFFF"));
                    tvAddress.setTextColor(Color.parseColor("#FFFFFF"));
                } else {
                    helper.setBackgroundRes(R.id.cl_main, R.drawable.bg_grey_8);
                    tvName.setTextColor(Color.parseColor("#000000"));
                    tvPhone.setTextColor(Color.parseColor("#000000"));
                    tvAddress.setTextColor(Color.parseColor("#000000"));
                }
            }
        };
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (Outlets datum : mAdapter.getData()) {
                    datum.setChecked(false);
                }
                Outlets data = mAdapter.getData().get(position);
                data.setChecked(true);
                listener.onClick(String.valueOf(data.getOutlets_id()), data.getOutlets_name());
                mAdapter.notifyDataSetChanged();
                dismiss();
            }
        });
    }

    private List<Outlets> list;
    private boolean isNewData = false;

    public void setList(List<Outlets> list) {
        this.list = list;
        isNewData = true;
    }

    private MyClickListener2 listener;

    public void setMyClickListener(@Nullable MyClickListener2 l) {
        this.listener = l;
    }

    @Override
    public void show() {
        super.show();
        if (isNewData) {
            isNewData = false;
            mAdapter.setNewData(list);
        }
    }
}
